import { _decorator, Component, Node, Toggle, UITransform, Label, find, Tween, Vec3, tween } from 'cc';
import { Constant } from '../Common/Constant';
import StorageManager from '../Common/StorageManager';
import { UIManager } from '../Manager/UIManager';
const { ccclass, property } = _decorator;

@ccclass('PrivacyPanel')
export class PrivacyPanel extends Component {

    scrollView: Node = null!;
    toggle: Toggle = null!;
    contentNd: Node = null!;
    contentUITranform: UITransform = null!;

    height: number = 0;

    onLoad() {
        this.scrollView = find("ScrollView", this.node);
        this.toggle = find("ScrollView/Toggle", this.node).getComponent(Toggle);
        this.contentNd = find("ScrollView/view/content", this.node);
        this.contentUITranform = this.contentNd.getComponent(UITransform);
    }

    start() {
        this.height = 0;
        this.GetAllFirstChild(this.contentNd);
        this.contentUITranform.setContentSize(this.contentUITranform.contentSize.width, this.height + 100);
    }

    Show() {
        this.toggle.isChecked = StorageManager.GetBool(Constant.Key.AgreePolicy);
        Tween.stopAllByTarget(this.scrollView);
        this.scrollView.scale = Vec3.ZERO;

        tween(this.scrollView).to(1.5, { scale: Vec3.ONE }, { easing: `quartOut` }).call(() => {

        }).start();
    }

    GetAllFirstChild(node: Node) {
        if (node && node.children.length > 0) {
            this.height += node.children[0].getComponent(UITransform).contentSize.height;
            this.GetAllFirstChild(node.children[0]);
        } else {
            return;
        }
    }

    OnButtonClick() {
        if (this.toggle.isChecked) {
            StorageManager.SetBool(Constant.Key.AgreePolicy, true);
            UIManager.Instance.HidePanel(Constant.Panel.PrivacyPanel);
        } else {
            UIManager.ShowTip("您需要先勾选同意隐私政策。");
        }
    }

}
